package media;

import java.io.IOException;

import Utils.StackTraceUtil;
import android.media.MediaRecorder;

import com.netease.log.NTLog;

/**
 * 封装录音功能
 */
public class MediaRecorderWrapper {
    public static final String TAG = MediaRecorderWrapper.class.getSimpleName();
	private boolean bStart = false;
	private long mStartTime = -1;
	private long mStopTime = -1;
	
	private String mFilePath;
	private MediaRecorder mRecord;
	private Thread mThread;
	private MediaRecorderListener mMediaRecorderListener;
	private static MediaRecorderWrapper sInstance = null;

	synchronized public static MediaRecorderWrapper getInstance() {
        if (null == sInstance) {
            sInstance = new MediaRecorderWrapper();
        }
        
        return sInstance;
    }
	
	private MediaRecorderWrapper() {
	}

	public void setFilePath(String filePath) {
        mFilePath = filePath;
    }
	
	public String getFilePath() {
	    return mFilePath;
	}
	
	public void setListener(MediaRecorderListener l) {
	    mMediaRecorderListener = l;
	}
	
	/**
	 * 获取录音时长
	 * @return
	 */
	public long getDuration() {
	    if (mStopTime > 0 && mStartTime > 0) {
	        return mStopTime - mStartTime;
	    }
	    
	    return -1;
	}
	
	public synchronized boolean start() {
		boolean bRes = false;
		if (bStart) {
			return bRes;
		}
		mRecord = new MediaRecorder();
		mRecord.setAudioSource(MediaRecorder.AudioSource.MIC);
		mRecord.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
		mRecord.setOutputFile(mFilePath);
		mRecord.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

		try {
			mRecord.prepare();
			mRecord.start();
			bStart = true;
			bRes = true;
			mStartTime = System.currentTimeMillis();
			mStopTime = -1;
		} catch (IllegalStateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		mThread = new Thread(new Runnable() {
            
            @Override
            public void run() {
                while (bStart) {
                    try {
                        Thread.sleep(20);
                        if (null != mRecord) {
                            final int maxAmplitude = mRecord.getMaxAmplitude();
                            NTLog.d(TAG, StackTraceUtil.getMethodName() + " getMaxAmplitude()=" + maxAmplitude);
                            if (null != mMediaRecorderListener) {
                                mMediaRecorderListener.onMaxAmplitude(maxAmplitude);
                            }
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } catch (IllegalStateException e) {
                        e.printStackTrace();
                    } catch (RuntimeException e) {
                        e.printStackTrace();
                    }
                }
            }
        }, TAG);
		mThread.start();
		return bRes;
	}

	public synchronized void stop() {
	    bStart = false;
		if (mRecord != null) {
			mRecord.stop();
			mRecord.release();
		}
		mRecord = null;
		mStopTime = System.currentTimeMillis();
	}
	
	public boolean isRecord() {
	    return bStart;
	}
	
	public interface MediaRecorderListener {
        public void onMaxAmplitude(int value);
        
    }
}
